import * as types from '../mutation-type'
import {getMyPreofile} from '../../service/api'

let userId = window.sessionStorage.userId || ''
// <!--initial state(状态)-->
const state = {
  userId: userId,
  token: null,
  userPhone: '',
  userEmail: '',
  userPassword: '',
  ziyueId: '',
  avatar: ''
}
console.log('state', state)

// getters（接收器）
const getters = {
  token: state => state.token,
  userPhone: state => state.userPhone,
  userPassword: state => state.userPassword,
  userEmail: state => state.userEmail,
  ziyueId: state => state.ziyueId,
  avatar: state => state.avatar
}
console.log('getters', getters)

// actions(动作)
const actions = {
  recordUserInfo ({commit, state}, datas) {
    commit(types.RECORD_USERINFO, datas)
  },
  async getMyPreofile ({commit, state}) {
    console.log('userid', state.userId)
    if (!state.userId || state.ziyueId) return

    let {code, value} = await getMyPreofile(state.userId)
    if (code === 200) {
      commit(types.RECORD_MYINFO, value)
    }
  }
}
console.log('actions', actions)

// mutations(变化)
const mutations = {
  // 缓存登录信息
  [types.RECORD_USERINFO] (state, datas) {
    state.userId = datas.id
    state.token = datas.token

    state.chatUsername = datas.chatUsername
    state.chatPassword = datas.chatPassword

    window.localStorage.userId = datas.id
    window.localStorage.token = datas.token
    window.localStorage.chatUsername = datas.chatUsername
    window.localStorage.chatPassword = datas.chatPassword
  },
  [types.CLEAR_USERINFO] (state) {
    // 清除用户信息
    state.token = null
    state.userId = ''
    state.ziyueId = ''
    state.userPhone = ''
    state.userPassword = ''
    state.userEmail = ''
    state.avatar = ''

    window.localStorage.removeItem('userId')
    window.localStorage.removeItem('token')
    window.localStorage.removeItem('chatUsername')
    window.localStorage.removeItem('chatPassword')
  },
  [types.RECORD_MYINFO] (state, datas) {
    // 更改我的信息
    state.ziyueId = datas.ziyueId
    state.userPhone = datas.phone
    state.userPassword = datas.password
    state.userEmail = datas.email
    state.avatar = datas.headPic
  },
  [types.UPDATE_AVATAR] (state, avatar) {
    state.avatar = avatar
  },
  [types.UPDATE_PHONE] (state, phone) {
    state.userPhone = phone
  },
  [types.UPDATE_PASSWORD] (state, password) {
    state.userPassword = password
  }
}
console.log('mutations', mutations)
// console.log('mutations', mutations.types.password)
export default {
  state,
  getters,
  actions,
  mutations
}

// const state = {
//   userId: userId,
//   token: null,
//   myIntegral: '',
//   nickname: '',
//   ziyueId: '',
//   avatar: '',
//   roles: [],
//   userQQ: '',
//   userWeixin: '',
//   userPhone: '',
//   userEmail: ''
// }

// const getters = {
//   userId: state => state.userId,
//   token: state => state.token,
//   myIntegral: state => state.myIntegral,
//   nickname: state => state.nickname,
//   ziyueId: state => state.ziyueId,
//   avatar: state => state.avatar,
//   roles: state => state.roles,
//   userQQ: state => state.userQQ,
//   userWeixin: state => state.userWeixin,
//   userPhone: state => state.userPhone,
//   userEmail: state => state.userEmail
// }

// const actions = {
//   myIntegral ({commit, state}) {
//     getMyIntegral().then(res => {
//       commit(types.RECORD_MYINTEGRAL, res.value)
//     });
//   },
//   recordUserInfo ({commit, state}, datas) {
//     commit(types.RECORD_USERINFO, datas)
//   },
//   async getMyPreofile ({commit, state}) {
//     console.log(state.userId)
//     if (!state.userId || state.ziyueId) return

//     let {code, value} = await getMyPreofile(state.userId)
//     if (code === 200) {
//       commit(types.RECORD_MYINFO, value)
//     }
//   }
// }

// const mutations = {
//   [types.RECORD_USERINFO] (state, datas) {
//     state.userId = datas.id
//     state.token = datas.token
//     state.chatUsername = datas.chatUsername
//     state.chatPassword = datas.chatPassword

//     window.localStorage.userId = datas.id
//     window.localStorage.token = datas.token
//     window.localStorage.chatUsername = datas.chatUsername
//     window.localStorage.chatPassword = datas.chatPassword
//   },
//   [types.CLEAR_USERINFO] (state) {
//     state.userId = ''
//     state.token = null
//     state.chatUsername = ''
//     state.chatPassword = ''
//     state.nickname = ''
//     state.ziyueId = ''
//     state.myIntegral = ''
//     state.avatar = ''
//     state.roles = []
//     state.userQQ = ''
//     state.userWeixin = ''
//     state.userPhone = ''
//     state.userEmail = ''

//     window.localStorage.removeItem('userId')
//     window.localStorage.removeItem('token')
//     window.localStorage.removeItem('chatUsername')
//     window.localStorage.removeItem('chatPassword')
//   },
//   [types.RECORD_MYINTEGRAL] (state, datas) {
//     state.myIntegral = datas.value
//   },
//   [types.RECORD_MYINFO] (state, datas) {
//     state.nickname = datas.nickName || datas.name
//     state.ziyueId = datas.ziyueId
//     state.myIntegral = datas.ziyueBean
//     state.avatar = datas.headPic
//     state.roles = datas.partyRoles
//     state.userQQ = datas.qq
//     state.userWeixin = datas.weixin
//     state.userPhone = datas.phone
//     state.userEmail = datas.email
//   },
//   [types.UPDATE_AVATAR] (state, avatar) {
//     state.avatar = avatar
//   },
//   [types.UPDATE_NICKNAME] (state, nickname) {
//     state.nickname = nickname
//   },
//   [types.UPDATE_PHONE] (state, phone) {
//     state.userPhone = phone
//   }
// }

// export default {
//   state,
//   getters,
//   actions,
//   mutations
// }
